//package com.hk.blog.service.impl;
//
//import com.hk.blog.domain.es.EsArticleInfo;
//import com.hk.blog.repository.elasticsearch.EsArticleInfoRepository;
//import com.hk.blog.service.CategoryInfoService;
//import com.hk.blog.service.EsArticleInfoService;
//import com.hk.blog.vo.ArticleListVo;
//import com.hk.commons.util.ObjectUtils;
//import com.hk.commons.util.SnowflakeIdGenerator;
//import com.hk.core.elasticsearch.repository.BaseElasticsearchRepository;
//import com.hk.core.page.QueryPage;
//import com.hk.core.query.QueryModel;
//import com.hk.core.service.elasticsearch.impl.ElasticSearchServiceImpl;
//import com.hk.core.service.exception.ServiceException;
//import lombok.RequiredArgsConstructor;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDateTime;
//
//@Service
//@RequiredArgsConstructor
//public class EsArticleInfoServiceImpl extends ElasticSearchServiceImpl<EsArticleInfo>
//        implements EsArticleInfoService {
//
//    private final EsArticleInfoRepository esArticleInfoRepository;
//
//    private final CategoryInfoService categoryInfoService;
//
//    @Override
//    public QueryPage<ArticleListVo> findForPage(QueryModel<EsArticleInfo> queryModel) {
//        return esArticleInfoRepository.findForPage(queryModel);
//    }
//
//    @Override
//    protected BaseElasticsearchRepository<EsArticleInfo> getBaseRepository() {
//        return esArticleInfoRepository;
//    }
//
//    @Override
//    @Async
//    public void deleteById(String id) {
//        var articleInfo = esArticleInfoRepository.findById(id)
//                .orElseThrow(() -> new ServiceException("文章不存在"));
//        categoryInfoService.subtractArticleNum(articleInfo.getCategoryId(), 1);
//        esArticleInfoRepository.delete(articleInfo);
//    }
//
//    @Override
//    @Transactional
//    public EsArticleInfo insert(EsArticleInfo esArticleInfo) {
//        var categoryInfo = categoryInfoService.findById(esArticleInfo.getCategoryId())
//                .orElseThrow(() -> new ServiceException("类别不存在"));
//        categoryInfo.setArticleNum(ObjectUtils.defaultIfNull(categoryInfo.getArticleNum(), 0) + 1);
//        esArticleInfo.setBrowseNum(0);
//        categoryInfoService.updateById(categoryInfo);
//        esArticleInfo.setId(new SnowflakeIdGenerator().generate().toString());
//        esArticleInfo.setLastModifiedDate(LocalDateTime.now());
//        return super.insert(esArticleInfo);
//    }
//
//    @Override
//    @Transactional
//    public EsArticleInfo updateByIdSelective(EsArticleInfo esArticleInfo) {
//        var find = esArticleInfoRepository.findById(esArticleInfo.getId())
//                .orElseThrow(() -> new ServiceException("文章不存在"));
//        if (!find.getCategoryId().equals(esArticleInfo.getCategoryId())) {
//            categoryInfoService.subtractArticleNum(find.getCategoryId(), 1);
//            categoryInfoService.addArticleNum(esArticleInfo.getCategoryId(), 1);
//        }
//        esArticleInfo.setLastModifiedDate(LocalDateTime.now());
//        return super.updateByIdSelective(esArticleInfo);
//    }
//}
